package org.jeecg.modules.paycenter.channel.zzbank.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.PayConstants;
import org.jeecg.modules.paycenter.channel.constant.CommonConstants;
import org.jeecg.modules.paycenter.channel.zzbank.service.IHyZzbankAccountService;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.wlhy.bill.entity.HyDriverBill;
import org.jeecg.modules.wlhy.bill.service.IHyDriverBillService;
import org.jeecg.modules.wlhy.utils.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
@EnableScheduling
public class ZhengzhouBankCashOutTask {

    @Resource
    private IHyDriverBillService hyDriverBillService;
    @Resource
    private IHyZzbankAccountService accountService;

    @Scheduled(cron = "${scheduling.cashoutjob:-}")
    private void downloadReceipt() throws Exception {
        log.info("=====================定时任务开始查询提现状态===================");
        List<HyDriverBill> inprocessList = hyDriverBillService.lambdaQuery()
                .eq(HyDriverBill::getPayChannel, PayChannelEnum.ZHENGZHOU.channelType)
                .eq(HyDriverBill::getBillType, PayConstants.BILL_TYPE.CASH_OUT)
                .ne(HyDriverBill::getRespCode, PayConstants.BANK_PAY_SOUR.SUCCESS)
                .list();

        if (CollUtil.isNotEmpty(inprocessList)) {
            for (HyDriverBill driverBill : inprocessList) {
                //根据流水号查询交易结果，交易结果回返回成交流水号
                String startDate = DateUtil.format(DateUtil.offsetDay(driverBill.getCreateTime(), -1), "yyyy-MM-dd");
                String endDate = DateUtil.format(DateUtil.offsetDay(driverBill.getCreateTime(), 1), "yyyy-MM-dd");
                log.info("开始查询交易数据---交易流水号billNum为："+driverBill.getBillNum());
                Map<String, String> transResultMap = accountService.queryTransferResult(driverBill.getBillNum(), driverBill.getFrontFlowNo(),driverBill.getDbaccno(), "1", startDate, endDate);
                log.info("开始查询交易数据结果为---"+transResultMap.toString());
                if (ObjUtil.isNotEmpty(transResultMap)) {
                    String transStatus = transResultMap.get("TransStatus");
                    if ("0".equals(transStatus)) {//交易成功
                        driverBill.setRespSource(CommonConstants.SUCCESS.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.SUCCESS);
                        driverBill.setRespInfo("提现成功");
                    } else if ("1".equals(transStatus)) {
                        driverBill.setRespSource(CommonConstants.FAIL.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现失败");
                    } else if ("2".equals(transStatus)) {
                        driverBill.setRespSource(CommonConstants.FAIL.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现异常，状态未名");
                    } else if ("3".equals(transStatus)) {
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-在途");
                    }else if ("4".equals(transStatus)) {
                        driverBill.setRespSource(CommonConstants.FAIL.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-不存在");
                    }else if ("5".equals(transStatus)) {
                        driverBill.setRespSource(CommonConstants.FAIL.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-转代付账户失败");
                    }else if ("6".equals(transStatus)) {
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-转代付账户成功");
                    }else if ("7".equals(transStatus)) {
                        driverBill.setRespSource(CommonConstants.FAIL.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-转代付账户成功，转收款账户失败");
                    }else if ("8".equals(transStatus)) {
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-转代付账户成功，转收款账户成功");
                    }else if ("9".equals(transStatus)) {
                        driverBill.setRespSource(CommonConstants.FAIL.toString());
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-转代付账户成功，转收款账户处理中");
                    }else if ("10".equals(transStatus)) {
                        driverBill.setRespCode(PayConstants.BANK_PAY_SOUR.FAIL);
                        driverBill.setRespInfo("提现状态-待银行审核");
                    }
                    hyDriverBillService.lambdaUpdate()
                            .set(HyDriverBill::getRespSource, driverBill.getRespSource())
                            .set(HyDriverBill::getRespCode, driverBill.getRespCode())
                            .set(HyDriverBill::getRespInfo, driverBill.getRespInfo())
                            .eq(HyDriverBill::getId, driverBill.getId())
                            .update();
                }
            }
        }
        log.info("=====================定时任务查询提现状态结束=================");

    }
}
